//
//  LHAddImageViewCell.m
//  Loohoo
//
//  Created by 陈镇 on 2025/6/23.
//

#import "LHAddImageViewCell.h"


@interface LHAddImageViewCell()

@property(nonatomic,strong) QMUIButton *addBtn;
@property(nonatomic,strong) QMUIButton *deleteBtn;
@property(nonatomic,strong) UIImageView *selectImage;


@end
@implementation LHAddImageViewCell


-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
   
        [self addSubview:self.addBtn];
        [self addSubview:self.selectImage];
        [self addSubview:self.deleteBtn];

    }
    return self;
}


-(QMUIButton *)deleteBtn{
    if (!_deleteBtn) {
        _deleteBtn = [QMUIButton createButtonImageName:@"img_seller_img_del" selectImageName:@"img_seller_img_del"];
        [_deleteBtn addTarget:self action:@selector(clickDelete) forControlEvents:UIControlEventTouchUpInside];
    }
    return _deleteBtn;
}

-(UIImageView *)selectImage{
    if (!_selectImage) {
        _selectImage = [[UIImageView alloc] init];
        _selectImage.contentMode = UIViewContentModeScaleAspectFill;
        _selectImage.layer.masksToBounds = YES;
        
    }
    return _selectImage;
}
-(QMUIButton *)addBtn{
    if (!_addBtn) {
        _addBtn = [QMUIButton createButtonTitle:@"商品主图" titleColor:UIColorHex(#999999) font:SLFontMakeRegular(10)];
        [_addBtn setImage:[UIImage imageNamed:@"icon_seller_img"] forState:UIControlStateNormal];
        _addBtn.imagePosition = QMUIButtonImagePositionTop;
        _addBtn.backgroundColor = UIColorHex(#F3F3F3);
        [_addBtn addTarget:self action:@selector(clickAdd) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addBtn;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    _addBtn.frame = self.bounds;
    _selectImage.frame =  self.bounds;
    
    _deleteBtn.frame = CGRectMake(self.width - 15, 0, 15, 15);
}

-(void)setModel:(LHAddImageCellModel *)model{
    _model = model;
    
    _addBtn.hidden = !model.isSelectAdd;
    _deleteBtn.hidden = model.isSelectAdd;
    _selectImage.hidden = model.isSelectAdd;
  
    
    if (model.isSelectAdd) {
        if (model.count > 0) {
            NSString *title = [NSString stringWithFormat:@"%@/%@",@(model.count),@(model.maxCount)];
            [self.addBtn setTitle:title forState:UIControlStateNormal];
        }else{
            [self.addBtn setTitle:model.btnTitle forState:UIControlStateNormal];
        }
    }else{
        
        self.selectImage.image = model.selectImage;
    }
    
}


-(void)clickAdd{
    
    
    if (_delegate && [_delegate respondsToSelector:@selector(didselectAdd)]) {
        [_delegate didselectAdd];
    }
    
}
-(void)clickDelete{
    if (_delegate && [_delegate respondsToSelector:@selector(didDeleteBtn:)]) {
        [_delegate didDeleteBtn:_model];
    }
}

@end
